{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "8860d09f",
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "bd9af7ca",
   "metadata": {},
   "outputs": [],
   "source": [
    "df = pd.read_csv(\"./datas/beijing_tianqi/beijing_tianqi_2017-2019.csv\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "d291fc6d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>ymd</th>\n",
       "      <th>bWendu</th>\n",
       "      <th>yWendu</th>\n",
       "      <th>tianqi</th>\n",
       "      <th>fengxiang</th>\n",
       "      <th>fengli</th>\n",
       "      <th>aqi</th>\n",
       "      <th>aqiInfo</th>\n",
       "      <th>aqiLevel</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>2017-01-01</td>\n",
       "      <td>5℃</td>\n",
       "      <td>-3℃</td>\n",
       "      <td>霾~晴</td>\n",
       "      <td>南风</td>\n",
       "      <td>1-2级</td>\n",
       "      <td>450</td>\n",
       "      <td>严重污染</td>\n",
       "      <td>6</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2017-01-02</td>\n",
       "      <td>7℃</td>\n",
       "      <td>-6℃</td>\n",
       "      <td>晴~霾</td>\n",
       "      <td>南风</td>\n",
       "      <td>1-2级</td>\n",
       "      <td>246</td>\n",
       "      <td>重度污染</td>\n",
       "      <td>5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>2017-01-03</td>\n",
       "      <td>5℃</td>\n",
       "      <td>-5℃</td>\n",
       "      <td>霾</td>\n",
       "      <td>南风</td>\n",
       "      <td>1-2级</td>\n",
       "      <td>320</td>\n",
       "      <td>严重污染</td>\n",
       "      <td>6</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>2017-01-04</td>\n",
       "      <td>6℃</td>\n",
       "      <td>-5℃</td>\n",
       "      <td>霾</td>\n",
       "      <td>北风</td>\n",
       "      <td>1-2级</td>\n",
       "      <td>360</td>\n",
       "      <td>严重污染</td>\n",
       "      <td>6</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>2017-01-05</td>\n",
       "      <td>2℃</td>\n",
       "      <td>-4℃</td>\n",
       "      <td>霾</td>\n",
       "      <td>北风</td>\n",
       "      <td>1-2级</td>\n",
       "      <td>280</td>\n",
       "      <td>重度污染</td>\n",
       "      <td>5</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "          ymd bWendu yWendu tianqi fengxiang fengli  aqi aqiInfo  aqiLevel\n",
       "0  2017-01-01     5℃    -3℃    霾~晴        南风   1-2级  450    严重污染         6\n",
       "1  2017-01-02     7℃    -6℃    晴~霾        南风   1-2级  246    重度污染         5\n",
       "2  2017-01-03     5℃    -5℃      霾        南风   1-2级  320    严重污染         6\n",
       "3  2017-01-04     6℃    -5℃      霾        北风   1-2级  360    严重污染         6\n",
       "4  2017-01-05     2℃    -4℃      霾        北风   1-2级  280    重度污染         5"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "d34bc0d2",
   "metadata": {},
   "outputs": [],
   "source": [
    "df[\"bWendu\"] = df[\"bWendu\"].map(lambda x : int(str(x).replace(\"℃\", \"\")))\n",
    "df[\"yWendu\"] = df[\"yWendu\"].map(lambda x : int(str(x).replace(\"℃\", \"\")))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "66317b06",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>ymd</th>\n",
       "      <th>bWendu</th>\n",
       "      <th>yWendu</th>\n",
       "      <th>tianqi</th>\n",
       "      <th>fengxiang</th>\n",
       "      <th>fengli</th>\n",
       "      <th>aqi</th>\n",
       "      <th>aqiInfo</th>\n",
       "      <th>aqiLevel</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>2017-01-01</td>\n",
       "      <td>5</td>\n",
       "      <td>-3</td>\n",
       "      <td>霾~晴</td>\n",
       "      <td>南风</td>\n",
       "      <td>1-2级</td>\n",
       "      <td>450</td>\n",
       "      <td>严重污染</td>\n",
       "      <td>6</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2017-01-02</td>\n",
       "      <td>7</td>\n",
       "      <td>-6</td>\n",
       "      <td>晴~霾</td>\n",
       "      <td>南风</td>\n",
       "      <td>1-2级</td>\n",
       "      <td>246</td>\n",
       "      <td>重度污染</td>\n",
       "      <td>5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>2017-01-03</td>\n",
       "      <td>5</td>\n",
       "      <td>-5</td>\n",
       "      <td>霾</td>\n",
       "      <td>南风</td>\n",
       "      <td>1-2级</td>\n",
       "      <td>320</td>\n",
       "      <td>严重污染</td>\n",
       "      <td>6</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>2017-01-04</td>\n",
       "      <td>6</td>\n",
       "      <td>-5</td>\n",
       "      <td>霾</td>\n",
       "      <td>北风</td>\n",
       "      <td>1-2级</td>\n",
       "      <td>360</td>\n",
       "      <td>严重污染</td>\n",
       "      <td>6</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>2017-01-05</td>\n",
       "      <td>2</td>\n",
       "      <td>-4</td>\n",
       "      <td>霾</td>\n",
       "      <td>北风</td>\n",
       "      <td>1-2级</td>\n",
       "      <td>280</td>\n",
       "      <td>重度污染</td>\n",
       "      <td>5</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "          ymd  bWendu  yWendu tianqi fengxiang fengli  aqi aqiInfo  aqiLevel\n",
       "0  2017-01-01       5      -3    霾~晴        南风   1-2级  450    严重污染         6\n",
       "1  2017-01-02       7      -6    晴~霾        南风   1-2级  246    重度污染         5\n",
       "2  2017-01-03       5      -5      霾        南风   1-2级  320    严重污染         6\n",
       "3  2017-01-04       6      -5      霾        北风   1-2级  360    严重污染         6\n",
       "4  2017-01-05       2      -4      霾        北风   1-2级  280    重度污染         5"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.head()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d1b6093d",
   "metadata": {},
   "source": [
    "### 同时添加温差，平均温度"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "5bca4711",
   "metadata": {},
   "outputs": [],
   "source": [
    "def my_func(row):\n",
    "    return row[\"bWendu\"]-row[\"yWendu\"], (row[\"bWendu\"]+row[\"yWendu\"])/2\n",
    "\n",
    "df[[\"wencha\", \"avg\"]] = df.apply(my_func, axis=1, result_type=\"expand\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "efca8d95",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>ymd</th>\n",
       "      <th>bWendu</th>\n",
       "      <th>yWendu</th>\n",
       "      <th>tianqi</th>\n",
       "      <th>fengxiang</th>\n",
       "      <th>fengli</th>\n",
       "      <th>aqi</th>\n",
       "      <th>aqiInfo</th>\n",
       "      <th>aqiLevel</th>\n",
       "      <th>wencha</th>\n",
       "      <th>avg</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>2017-01-01</td>\n",
       "      <td>5</td>\n",
       "      <td>-3</td>\n",
       "      <td>霾~晴</td>\n",
       "      <td>南风</td>\n",
       "      <td>1-2级</td>\n",
       "      <td>450</td>\n",
       "      <td>严重污染</td>\n",
       "      <td>6</td>\n",
       "      <td>8.0</td>\n",
       "      <td>1.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2017-01-02</td>\n",
       "      <td>7</td>\n",
       "      <td>-6</td>\n",
       "      <td>晴~霾</td>\n",
       "      <td>南风</td>\n",
       "      <td>1-2级</td>\n",
       "      <td>246</td>\n",
       "      <td>重度污染</td>\n",
       "      <td>5</td>\n",
       "      <td>13.0</td>\n",
       "      <td>0.5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>2017-01-03</td>\n",
       "      <td>5</td>\n",
       "      <td>-5</td>\n",
       "      <td>霾</td>\n",
       "      <td>南风</td>\n",
       "      <td>1-2级</td>\n",
       "      <td>320</td>\n",
       "      <td>严重污染</td>\n",
       "      <td>6</td>\n",
       "      <td>10.0</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>2017-01-04</td>\n",
       "      <td>6</td>\n",
       "      <td>-5</td>\n",
       "      <td>霾</td>\n",
       "      <td>北风</td>\n",
       "      <td>1-2级</td>\n",
       "      <td>360</td>\n",
       "      <td>严重污染</td>\n",
       "      <td>6</td>\n",
       "      <td>11.0</td>\n",
       "      <td>0.5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>2017-01-05</td>\n",
       "      <td>2</td>\n",
       "      <td>-4</td>\n",
       "      <td>霾</td>\n",
       "      <td>北风</td>\n",
       "      <td>1-2级</td>\n",
       "      <td>280</td>\n",
       "      <td>重度污染</td>\n",
       "      <td>5</td>\n",
       "      <td>6.0</td>\n",
       "      <td>-1.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "          ymd  bWendu  yWendu tianqi fengxiang fengli  aqi aqiInfo  aqiLevel  \\\n",
       "0  2017-01-01       5      -3    霾~晴        南风   1-2级  450    严重污染         6   \n",
       "1  2017-01-02       7      -6    晴~霾        南风   1-2级  246    重度污染         5   \n",
       "2  2017-01-03       5      -5      霾        南风   1-2级  320    严重污染         6   \n",
       "3  2017-01-04       6      -5      霾        北风   1-2级  360    严重污染         6   \n",
       "4  2017-01-05       2      -4      霾        北风   1-2级  280    重度污染         5   \n",
       "\n",
       "   wencha  avg  \n",
       "0     8.0  1.0  \n",
       "1    13.0  0.5  \n",
       "2    10.0  0.0  \n",
       "3    11.0  0.5  \n",
       "4     6.0 -1.0  "
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "cde48f1d",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
